package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.domain.User;
import com.example.config.auth.SecurityUtil;
import com.example.config.exception.GlobalExcetion;
import com.example.domain.BusFriendGroup;
import com.example.domain.vo.BusFriendGroupVo;
import com.example.feign.UserFeignService;
import com.example.ribbon.service.FriendGroupRibbonService;
import com.example.service.BusFriendGroupService;
import com.example.system.domain.PageData;
import com.example.util.Results;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Security;

@RestController
@RequestMapping("/busfreindgroup")
public class BusFriendGroupController {

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private BusFriendGroupService busFriendGroupService;

    @Autowired
    private FriendGroupRibbonService friendGroupRibbonService;

    @PostMapping("/save")
    public JSONObject save(@Validated @RequestBody BusFriendGroupVo vo) throws GlobalExcetion {
        String uid=SecurityUtil.getUserId();
        if(vo.getFriendUid().equals(uid)) {
            throw new GlobalExcetion(1,"不可添加自己");
        }
        boolean isHas=busFriendGroupService.getBusFriendGroup(uid,vo.getFriendUid());
        if(isHas) {
            throw new GlobalExcetion(1,"已存在该朋友");
        }
        User user =userFeignService.getUserByUserId(vo.getFriendUid());
        if(user==null){
            return Results.error("用户不存在");
        }
        String friendNote= vo.getFriendNote()==null? user.getUsername():vo.getFriendNote();
        String userId= SecurityUtil.getUserId();
        BusFriendGroup busFriendGroup = new BusFriendGroup();
        BeanUtils.copyProperties(vo, busFriendGroup);
        busFriendGroup.setFriendNote(friendNote);
        busFriendGroup.setUserId(userId);
        busFriendGroupService.saveBusFriendGroup(busFriendGroup);
        friendGroupRibbonService.receiveFriendGroup(vo.getFriendUid(),userId);
        return Results.success("成功");
    }

    @GetMapping("/getone")
    public JSONObject getOne(@RequestParam("uuid")String uuid) throws GlobalExcetion {
        BusFriendGroup busFriendGroup=busFriendGroupService.getBusFriendGroupByUUID(uuid);
        return Results.success(busFriendGroup);
    }

    @GetMapping("page")
    public JSONObject page(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
        String uid=SecurityUtil.getUserId();
        PageData<BusFriendGroup> page=busFriendGroupService.pageBusFriendGroup(new Page(pageNum,pageSize),uid);
        return Results.success(page);
    }

    @PostMapping("/delete")
    public JSONObject delete(@RequestParam("uuid")String uuid) throws GlobalExcetion {
        busFriendGroupService.deleteBusFriendGroup(uuid);
        return Results.success("删除成功");
    }

    @PostMapping("/update")
    public JSONObject update(@RequestParam("uuid")String uuid,
                             @RequestParam("note")String note) throws GlobalExcetion {
        BusFriendGroup busFriendGroup=busFriendGroupService.getBusFriendGroupByUUID(uuid);
        busFriendGroup.setFriendNote(note);
        busFriendGroupService.updateBusFriendGroup(busFriendGroup);
        return Results.success("修改成功");
    }

    @PostMapping("receiveFriendGroup")
    public JSONObject receiveFriendGroup(@RequestParam("uid")String uid,
                                         @RequestParam("friendUid")String friendUid) throws GlobalExcetion {
        User user=userFeignService.getUserByUserId(uid);
        User friend=userFeignService.getUserByUserId(friendUid);
        if(user==null || friend==null) {
            throw new GlobalExcetion(1,"有用户id不存在于系统");
        }
        boolean isexist=busFriendGroupService.getBusFriendGroup(uid,friendUid);
        if(isexist) {
            throw new GlobalExcetion(1,"朋友已存在");
        }
        BusFriendGroup busFriendGroup=new BusFriendGroup();
        busFriendGroup.setUserId(uid);
        busFriendGroup.setFriendUid(friendUid);
        busFriendGroup.setFriendNote(friend.getUsername());
        busFriendGroupService.saveBusFriendGroup(busFriendGroup);
        return Results.success("保存成功");
    }
}
